var stream = require('stream')
var util = require('util')

function ReadStream() {
    stream.Readable.call(this)
}

util.inherits(ReadStream, stream.Readable)

ReadStream.prototype._read = function () {
    this.push("I");
    this.push("Love");
    this.push("Imooc");
    this.push(null); // 要加这一句，不会报错（当发送一个null时，才会停止）
}

function WriteStream() {
    stream.Writable.call(this)
    this._cached = new Buffer('')
}

util.inherits(WriteStream, stream.Writable)

WriteStream.prototype._write = function (chunk, encode, cb) {
    console.log(chunk.toString());
    cb();
}

function TransformStream() {
    stream.Transform.call(this);
}

util.inherits(TransformStream, stream.Transform)

TransformStream.prototype._transform = function (chunk, encode, cb) {
    this.push(chunk)
    cb()
}
var c = 97;
// transform 流等同于 中间处理的环节，处理好后交给write流输出打印
TransformStream.prototype._flush = function (cb) {
    debugger
    for(var i=0; i<10; i++){
        c++;
        this.push(c.toString());
        if (c == 100){
            this.push(null);
             break;
        }
    }
    cb()
}
var rs = new ReadStream();
var ws = new WriteStream();
var ts = new TransformStream();

rs.pipe(ts).pipe(ws)
